<!--
 * @author hemengxia
 活动管理--游戏活动
 -->
<template>
  <div id="game-activity-index">
    <el-collapse-transition>
      <div v-show="searchshow" class="course-search merge">
        <span class="svg-container">
          <svg-icon icon-class="search" />
        </span>
        <el-input
          v-model.trim="searchQuery.name"
          placeholder="请输入活动名称搜索"
          class="search-input"
          @keyup.enter.native="handleFilter"
        />
        <div class="course-search-btn">
          <el-button class="filter-item" type="primary" @click="handleFilter">搜索</el-button>
          <el-button class="filter-item" type="primary" @click="resettingBtn">重置</el-button>
        </div>
      </div>
    </el-collapse-transition>
    <div class="btn-container">
      <div class="btn-container-title">
        <span>游戏活动管理</span>
      </div>
      <div class="btn-container-btns">
        <el-button class="filter-item" style="margin-left: 10px;" @click="handleCreate">新建</el-button>
        <!--        <el-button class="filter-item" style="margin-left: 10px;" @click="handleDeletes">批量删除</el-button>-->
      </div>
    </div>
    <section class="main-table">
      <v-table :list="list" tag="gameActivity" :list-loading="listLoading" :filter-column="filtercolumn">
        <el-table-column label="操作" fixed="right" align="center" min-width="100" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="handleView(scope.row)">预览</el-button>
            <el-button
              type="text"
              size="mini"
              @click="handlePublish(scope.row)"
            >{{ scope.row.publishStatus === "RPAPUBSTATUS0" ? "发布" : "取消发布" }}
            </el-button>
            <el-button type="text" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button type="text" size="mini" @click="handleAddress(scope.row)">查看地址</el-button>
            <el-button type="text" size="mini" @click="handleConfig(scope.row)">配置</el-button>
            <el-button type="text" size="mini" @click="handleRecord(scope.row)">参与情况</el-button>
            <el-button type="text" size="mini" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </v-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </section>
    <preview
      v-if="previewStatus"
      :id="obj.id"
      :redpacket-activity-type="obj.redpacketActivityType"
      @closeSet="previewStatus = false"
    />
  </div>
</template>

<script>
import {
  queryPage,
  deleteById,
  updateState
} from '@/views/gameactivity/api/index'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
// eslint-disable-next-line no-unused-vars
import elDragDialog from '@/directive/el-drag-dialog'
import VTable from '@/components/Table'
import Preview from '@/views/gameactivity/components/preview'

export default {
  name: 'GameActivity',
  components: {
    Pagination,
    VTable,
    Preview
  },
  directives: {
    permission,
    elDragDialog
  },
  data() {
    return {
      submitloading: false,
      listQuery: {
        page: 1,
        limit: 10,
        ActivityType: 'ACTIVITY02'
      },

      searchshow: true,
      searchQuery: {
        name: ''
      },
      list: [],
      total: 0,
      multipleSelection: [],
      dictList: [],
      listLoading: false,
      previewStatus: false,
      obj: {}
    }
  },

  created() {
    this.getList()
  },
  methods: {
    /**
     * 预览
     */
    handleView(row) {
      if (!row.visitUrl) {
        this.$message({
          type: 'warning',
          message: '请先完成活动配置'
        })
        return
      }
      this.obj = Object.assign({}, row)
      this.previewStatus = true
    },
    /**
     * 启用/禁用
     */
    handlePublish(row) {
      this.$confirm(row.publishStatus === 'RPAPUBSTATUS1' ? '确认取消发布？' : '确认发布？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateState({ id: row.id }).then(res => {
          if (res.data.code === '200') {
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            this.getList()
          }
        })
      }).catch(() => {

      })
    },
    filtercolumn(scope, item) {
      if (item === 'cyrsrc') {
        return scope.row['joinPeopleNumber'] + '/' + scope.row['joinPeopleTime']
      } else {
        return scope.row[item]
      }
    },

    getList(obj) {
      this.listLoading = true
      var query = {
        page: obj ? obj.page : this.listQuery.page,
        pageSize: obj ? obj.limit : this.listQuery.limit,
        ActivityType: 'ACTIVITY02'
      }
      if (this.searchshow) {
        query['name'] = this.searchQuery.name
      }
      return new Promise((resolve, reject) => {
        queryPage(query).then(res => {
          this.listLoading = false
          this.list = res.data.data.rows || []
          this.total = res.data.data.total
        }).catch(error => {
          reject(error)
          this.listLoading = false
        })
      })
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resettingBtn() {
      this.listQuery.page = 1
      for (const key in this.searchQuery) {
        this.searchQuery[key] = ''
      }
      this.getList()
    },

    handleCreate() {
      this.$router.push({ path: this.$filterPath('manage'), query: { type: 'c' }})
    },
    handleUpdate(row) {
      this.$router.push({ path: this.$filterPath('manage'), query: { i: row.id, type: 'u' }})
    },
    handleConfig(row) {
      this.$router.push({
        path: this.$filterPath('config'),
        query: { i: row.id, redpacketActivityType: row.redpacketActivityType }
      })
    },
    /**
     * 参与情况
     * @param row
     */
    handleRecord(row) {
      this.$router.push({ path: this.$filterPath('record'), query: { i: row.id }})
    },
    handleDeletes() {
      const arr = []
      if (this.multipleSelection.length > 0) {
        this.multipleSelection.forEach(item => {
          arr.push(item.id)
        })
        const ids = arr.join(',')
        this.deleteUser(ids)
      } else {
        this.$message({
          message: '请选择需要删除的数据！',
          type: 'warning'
        })
      }
    },
    handleDelete(row) {
      this.deleteUser(row.id)
    },
    deleteUser(ids) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          deleteById({
            id: ids
          }).then(res => {
            this.$message.success('删除成功!')
            this.getList()
          })
        })
        .catch(() => {
        })
    },
    /**
     * 查看前端地址
     */
    handleAddress(row) {
      if (!row.visitUrl) {
        this.$message({
          type: 'warning',
          message: '请先完成活动配置'
        })
        return
      }
      this.$confirm(`链接地址为：${row.visitUrl}?i=${row.id}<span style="color:red">注意：该链接只对已发布数据有效</span>`, '提示', {
        dangerouslyUseHTMLString: true,
        confirmButtonText: '复制链接',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const input = document.createElement('input')
        input.setAttribute('readonly', 'readonly')
        input.setAttribute('value', row.visitUrl + '?id=' + row.id)
        document.body.appendChild(input)
        input.select()
        if (document.execCommand('copy')) {
          document.execCommand('copy')
          this.$message({
            title: '成功',
            message: '复制成功！',
            type: 'success',
            duration: 1500
          })
        }
        document.body.removeChild(input)
      }).catch(() => {

      })
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" type="text/scss">
$dark_gray: #bfced6;
// $mainColor: #18a5fd;

#game-activity-index {
  padding: 10px 20px 10px 10px;

  .course-search {
    background-color: #fff;
    padding: 0 10px;

    .svg-container {
      padding: 0 5px 0 10px;
      color: $dark_gray;
      vertical-align: middle;
      display: inline-block;

      &_login {
        font-size: 20px;
      }
    }

    .search-input {
      width: 60%;
      position: relative;

      &:after {
        content: "";
        position: absolute;
        left: -25px;
        right: 0;
        bottom: 8px;
        height: 1px;
        background: rgba(226, 227, 232, 1);
      }

      input {
        outline: none;
        background: transparent;
        border: none;
        outline: medium;
        padding: 0;

        &:focus {
          outline: none;
          background-color: transparent;
        }
      }
    }

    .course-search-btn {
      display: inline-block;
      float: right;

      .el-button.is-round {
        padding: 10px 30px;
        margin-right: 34px;
      }

      .course-search-more {
        font-size: 14px;
        font-weight: 500;
        @include font-color($mainColor);
        cursor: pointer;
        margin-left: 10px;
      }
    }

    &.merge {
      height: 50px;
      line-height: 50px;
    }

    &.separate {
      .course-search-inner {
        padding-top: 10px;

        .search-inner-item {
          .item-label {
            height: 30px;
            line-height: 30px;
            font-size: 14px;
            color: #a6a6a6;
            font-weight: 400;
          }

          .el-input {
            position: relative;

            .el-input__inner {
              border: none;
              padding: 0;
            }

            &:after {
              content: "";
              height: 1px;
              background-color: #e2e3e8;
              position: absolute;
              left: 0;
              right: 0;
              bottom: 0;
            }
          }
        }
      }

      .course-search-btn {
        text-align: center;
        display: block;
        float: none;
        height: 56px;
        line-height: 56px;
      }
    }
  }

  .btn-container {
    display: flex;
    align-items: center;
    margin-top: 5px;
    margin-bottom: 5px;

    .btn-container-title {
      flex: 1;
      height: 36px;
      line-height: 36px;
      font-size: 16px;
      color: rgba(51, 51, 51, 1);
    }
  }

  .dataForm {
    width: 100%;

    .el-radio__label {
      display: inline-block;
      max-width: 380px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }
  }

  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }

  .el-checkbox__label {
    font-weight: 400;
  }

  .el-form-item__label {
    // width: 90px !important;
  }

  .el-form-item__content {
    // margin-left: 90px !important;
  }
}
</style>
